﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Lin.Library
{
    /// <summary>
    /// Web窗体截图
    /// </summary>
    public class WebScreenshot
    {
        /// <summary>
        /// 站点URL
        /// </summary>
        public string WebsiteUrl { set; get; }
        /// <summary>
        /// 窗体宽度
        /// </summary>
        public int WindowWidth { set; get; }
        /// <summary>
        /// 窗体高度
        /// </summary>
        public int WindowHeight { set; get; }
        /// <summary>
        /// 区域X坐标
        /// </summary>
        public int AreaPostX { set; get; }
        /// <summary>
        /// 区域Y坐标
        /// </summary>
        public int AreaPostY { set; get; }
        /// <summary>
        /// 区域宽度
        /// </summary>
        public int AreaWidth { set; get; }
        /// <summary>
        /// 区域高度
        /// </summary>
        public int AreaHeight { set; get; }

        /// <summary>
        /// Web窗体截图构造
        /// </summary>
        public WebScreenshot() { }

        /// <summary>
        /// Web窗体截图构造
        /// </summary>
        /// <param name="websiteUrl">站点URL</param>
        /// <param name="windowWidth">窗体宽度</param>
        /// <param name="windowHeight">窗体高度</param>
        public WebScreenshot(string websiteUrl, int windowWidth, int windowHeight)
        {
            this.WebsiteUrl = websiteUrl;
            this.WindowWidth = windowWidth;
            this.WindowHeight = windowHeight;
        }

        /// <summary>
        /// Web窗体截图构造
        /// </summary>
        /// <param name="websiteUrl">站点URL</param>
        /// <param name="windowWidth">窗体宽度</param>
        /// <param name="windowHeight">窗体高度</param>
        /// <param name="areaPostX">区域X坐标</param>
        /// <param name="areaPostY">区域Y坐标</param>
        /// <param name="areaWidth">区域宽度</param>
        /// <param name="areaHeight">区域高度</param>
        public WebScreenshot(string websiteUrl, int windowWidth, int windowHeight, int areaPostX, int areaPostY, int areaWidth, int areaHeight)
        {
            this.WebsiteUrl = websiteUrl;
            this.WindowWidth = windowWidth;
            this.WindowHeight = windowHeight;
            this.AreaPostX = areaPostX;
            this.AreaPostY = areaPostY;
            this.AreaWidth = areaWidth;
            this.AreaHeight = areaHeight;
        }

        public Bitmap GetBitmap()
        {
            WebPageBitmap Shot = new WebPageBitmap(this.WebsiteUrl, this.WindowWidth, this.WindowHeight, this.AreaPostX, this.AreaPostY, this.AreaWidth, this.AreaHeight);

            return Shot.DrawBitmap();
        }
    }

    class WebPageBitmap
    {

        private WebBrowser SiteBrowser;
        private string WebsiteUrl = string.Empty;
        private int WindowWidth = 0;
        private int WindowHeight = 0;
        private int AreaPostX = 0;
        private int AreaPostY = 0;
        private int AreaWidth = 0;
        private int AreaHeight = 0;

        public WebPageBitmap(string websiteUrl, int windowWidth, int windowHeight, int areaPostX, int areaPostY, int areaWidth, int areaHeight)
        {
            this.WebsiteUrl = websiteUrl;
            this.WindowWidth = windowWidth;
            this.WindowHeight = windowHeight;
            this.AreaPostX = areaPostX;
            this.AreaPostY = areaPostY;
            this.AreaWidth = areaWidth;
            this.AreaHeight = areaHeight;

            SiteBrowser = new WebBrowser();
            SiteBrowser.ScrollBarsEnabled = false;
        }

        public Bitmap DrawBitmap()
        {
            SiteBrowser.Navigate(this.WebsiteUrl);

            while (SiteBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            try
            {
                SiteBrowser.Size = new Size(this.WindowWidth, this.WindowHeight);

                using (var bitmap = new Bitmap(this.WindowWidth, this.WindowHeight))
                {
                    var  drawRect = new Rectangle(0, 0, this.WindowWidth, this.WindowHeight);
                    SiteBrowser.DrawToBitmap(bitmap, drawRect);

                    return bitmap.Clone(new Rectangle(this.AreaPostX, this.AreaPostY, this.AreaWidth, this.AreaHeight), PixelFormat.Format64bppPArgb);
                }

            }
            catch
            {
                return null;
            }
            finally
            {
                SiteBrowser.Dispose();
                SiteBrowser = null;
            }
        }
    }

    /**
     * 调用
     *  private void Star()
        {
            Thread NewTh = new Thread(CaptureImage);
            NewTh.SetApartmentState(ApartmentState.STA);//必须启动单元线程
            NewTh.Start();
        }
     * 
     * public void CaptureImage()
        {
            //你的任务代码
            try
            {
                var webScreenshot = new WebScreenshot(WebsiteUrl, WindowWidth, WindowHeight, AreaPostX, AreaPostY, AreaWidth, AreaHeight);
                var image = webScreenshot.GetBitmap();//获取截图
                string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");

                image.Save(Server.MapPath(FileName + ".jpg"), ImageFormat.Jpeg);//保存截图到SnapPic目录下

            }
            catch (Exception ex)
            {
                //Todo
            }
        }
     * 
     * 
     **/
}